/**
 * Copyright (C), 2015-2020, XXX有限公司
 * FileName: BusCollectActivity
 * Author: Clark
 * Date: 2020/10/10 21:01
 * Description:
 * History:
 * <author> <time> <version> <desc>
 * 作者姓名 修改时间 版本号 描述
 */
package sz.szsmk.citizencard.ui.activity;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.cardview.widget.CardView;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.widget.ViewPager2;

import com.chenenyu.router.Router;
import com.chenenyu.router.annotation.Route;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.databinding.ActivityChargeMainBinding;
import sz.szsmk.citizencard.databinding.ActivityCollectionlistBinding;
import sz.szsmk.citizencard.ioc.Compontent.DaggerCommonActivityComponent;
import sz.szsmk.citizencard.model.network.bean.response.QueryNearStandbyRes;
import sz.szsmk.citizencard.model.network.bean.response.QueryUserCardRes;
import sz.szsmk.citizencard.mvp.contract.IChargeContact;
import sz.szsmk.citizencard.mvp.contract.ISimpleContact;
import sz.szsmk.citizencard.mvp.presenter.ChargePresenter;
import sz.szsmk.citizencard.mvp.presenter.SimplePresenter;
import sz.szsmk.citizencard.ui.adapter.BasePagerAdapter;
import sz.szsmk.citizencard.ui.adapter.BusCollectFragmentAdapter;
import sz.szsmk.citizencard.ui.adapter.CardPagerAdapter;
import sz.szsmk.citizencard.ui.adapter.CitizenCardvpAdatper;
import sz.szsmk.citizencard.ui.adapter.ContentAdapter;
import sz.szsmk.citizencard.ui.base.IBaseActivity;
import sz.szsmk.citizencard.ui.fragment.BusCollecLineFragment;
import sz.szsmk.citizencard.ui.fragment.BusHomeSiteCollecFragment;
import sz.szsmk.citizencard.ui.fragment.GoverFragment;
import sz.szsmk.citizencard.ui.newversion.activity.ScanQRCodeActivity;
import sz.szsmk.citizencard.ui.newversion.fragment.presenter.BusCodePresenter;
import sz.szsmk.citizencard.util.ACache;
import sz.szsmk.citizencard.util.Constant;
import sz.szsmk.citizencard.util.JumpThirdView;
import sz.szsmk.citizencard.util.JumpUtils;
import sz.szsmk.citizencard.util.SecureSharedPreferences;
import sz.szsmk.citizencard.widget.PureTextWebViewActivity;

/**
 * @ClassName: ChargeActivity
 * @Description:
 * @Author: Clark
 * @Date: 2020/10/10 21:01
 */
@RequiresApi(api = Build.VERSION_CODES.M)
@Route("rechargeActivity")
public class ChargeActivity extends IBaseActivity<ChargePresenter,
        ActivityChargeMainBinding> implements IChargeContact.IChargeView, View.OnClickListener {
    BasePagerAdapter basePagerAdapter;
    List<sz.szsmk.citizencard.model.network.bean.response.QueryUserCardRes.DataBean> list = new ArrayList<>();
    String access_token;
    TYReceiver tyReceiver;
    private CardView busCharge;


    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_charge_main;
    }

    @Override
    protected void initInjector() {
        DaggerCommonActivityComponent.builder().appCompontent(DaggerApplication.getAppComponent())
                .build().inject(this);
    }

    @Override
    protected void initViews() {
        showLoadingDialog();
        dataBinding.backLayout.settingTitle.setText("充值");
        busCharge = dataBinding.busCharge;
        //判断是否注册过公交卡
        String isRealName = ACache.get(this).getAsString("isRealName");
        if (isRealName.equals("1")){
            busCharge.setVisibility(View.VISIBLE);
        }
        dataBinding.backLayout.back.setOnClickListener(this);
//        basePagerAdapter = new BasePagerAdapter(list, R.layout.item_card_view, sz.szsmk.citizencard.BR.);
//         dataBinding.vpCardList.setAdapter(basePagerAdapter);
        dataBinding.vpCardList.setPageMargin(40);
        dataBinding.vpCardList.setOffscreenPageLimit(20);
        dataBinding.ziCardListIndicator.setViewPager(dataBinding.vpCardList);

        mPresenter.queryUserCard("3");//0 不去重 3是去重后的结果
        initViewPager();
        dataBinding.cardCloud.setOnClickListener(this);
        dataBinding.cardYl.setOnClickListener(this);
        dataBinding.cardCz.setOnClickListener(this);
        dataBinding.cardWallet.setOnClickListener(this);
        dataBinding.cardNfc.setOnClickListener(this);
        dataBinding.cardPaste.setOnClickListener(this);
        dataBinding.busCharge.setOnClickListener(this);
        //动态注册关闭接受者 用于接收广播关闭页面
        tyReceiver = new TYReceiver();
        IntentFilter itFilter = new IntentFilter();
        registerReceiver(tyReceiver, itFilter);




//        String bus_register = ACache.get(this).getAsString("bus_register");
//        if (bus_register!=null){
//            if (bus_register.equals("1")){
//                dataBinding.busCharge.setVisibility(View.VISIBLE);
//            }
//        }


    }

    @Override
    protected void updateViews() {

    }


    @Override
    public void onClick(View v) {
        Bundle bundle = new Bundle();
        int id = v.getId();
        if (id == R.id.back) {
            finish();
        } else if (id == R.id.card_cloud) {
            bundle.putString("url", Constant.BASE_URL + "app-front-end/#/charge/main?cardNo=" +
                    "" + "&access_token=" + access_token);
            JumpUtils.jumptoNatvie(ChargeActivity.this, "webView", 0, bundle);
        } else if (id == R.id.card_yl) {//                https://newapptest.szsmk.com/app-front-end/#/yongLian/main?access_token=381c9c4f3a97ce159b47c4809e4b42a3

            bundle.putString("url", Constant.BASE_URL + "app-front-end/#/yongLian/main?" + "&access_token=" + access_token);
            JumpUtils.jumptoNatvie(ChargeActivity.this, "webView", 0, bundle);
        } else if (id == R.id.card_cz) {//                https://newapptest.szsmk.com/app-front-end/#/shoppingMall/main?access_token=381c9c4f3a97ce159b47c4809e4b42a3
            bundle.putString("url", Constant.BASE_URL + "app-front-end/#/shoppingMall/main?" + "&access_token=" + access_token);
            JumpUtils.jumptoNatvie(ChargeActivity.this, "webView", 0, bundle);
        } else if (id == R.id.card_wallet) {//                bundle.putString("url", Constant.BASE_URL + "app-front-end/#/charge/main?cardNo=" +
//                        ""  + "&access_token=" + access_token);
//                JumpUtils.jumptoNatvie(ChargeActivity.this, "webView", 0, bundle);
            Toast.makeText(ChargeActivity.this, "功能开发中", Toast.LENGTH_SHORT).show();
        } else if (id == R.id.card_nfc) {
            Toast.makeText(ChargeActivity.this, "功能开发中", Toast.LENGTH_SHORT).show();
        } else if (id == R.id.card_paste) {//天谕sdk
            //JumpThirdView.baseToJumpView(this,"","PKACHONGZHI",0, "");
        } else if (id == R.id.bus_charge) {//逻辑问题 ，必须先请求
            mPresenter.getOpenId();
        }
    }

    CardPagerAdapter cardAdapter;

    //当前选中的 卡
    int currentPosition = 0;

    private void initViewPager() {
        access_token = ACache.get(this).getAsString("access_token");
        cardAdapter = new CardPagerAdapter(this, dataBinding.tabLayout);
        dataBinding.vpCardList.setAdapter(cardAdapter);
        dataBinding.vpCardList.setPageMargin(40);
        dataBinding.vpCardList.setOffscreenPageLimit(20);
        dataBinding.ziCardListIndicator.setViewPager(dataBinding.vpCardList);

        dataBinding.vpCardList.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                currentPosition = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        cardAdapter.setBaseInterface(new CardPagerAdapter.BaseInterface() {
            @Override
            public void onClick(View view, QueryUserCardRes.DataBean bean) {
                Bundle bundle = new Bundle();
                bundle.putString("url", Constant.BASE_URL + "app-front-end/#/charge/main?cardNo=" +
                        bean.getBindCardNo() + "&access_token=" + access_token);
                JumpUtils.jumptoNatvie(ChargeActivity.this, "webView", 0, bundle);
            }
        });

    }

    @Override
    public void getDataListSucc(QueryUserCardRes response) {
        dismissLoadingDialog();
        if(response.getData()!=null&&response.getData().size()>1){
            list.clear();
            dataBinding.noCard.setVisibility(View.GONE);
            list.addAll(response.getData());
            cardAdapter.setData(list);
            dataBinding.ziCardListIndicator.notifyView();
        }else {
            dataBinding.noCard.setVisibility(View.VISIBLE);
        }

    }

    @Override
    public void getDataListFail() {
        dismissLoadingDialog();

    }

    @Override
    public void getOpenIdSucc() {
        //跳转乘车二维码
        Router.build("dzsmkuser").with("type","1").go(this);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
         unregisterReceiver(tyReceiver);
    }

    public class TYReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            ChargeActivity.this.finish();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK){
        }
    }
}